package fms.models;
 
import java.util.List;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import fms.classes.Account;
 
 
public class AccountDAO {
 
    private SqlSessionFactory sqlSessionFactory;
 
    public AccountDAO(){
    	try{
    		sqlSessionFactory = MyBatisSqlConnection.getSqlSessionFactory();
    	}catch(Exception e) {
    		System.out.println("failed to get sqlSessionFactory");
    	}
    }
 
    /**Returns the list of all Account instances from the database*/
    @SuppressWarnings("unchecked")
    public List<Account> selectAll(){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            List<Account> list = session.selectList("Account.getAll");
            return list;
        } finally {
            session.close();
        }
    }
 
    /**Returns a Account instance from the database*/
    public Account selectById(int student_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            Account account = (Account) session.selectOne("Account.getById", student_id);
            return account;
        } finally {
            session.close();
        }
    }
 
    /**Updates an instance of Account in the database*/
    public void update(Account account){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.update("Account.update", account);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /**Insert an instance of Account into the database*/
    public void insert(Account account){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.insert("Account.insert", account);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /**Delete an instance of Account from the database*/
    public void delete(int student_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.delete("Account.deleteById", student_id);
            session.commit();
        } finally {
            session.close();
        }
    }
}